package com.cpo.dao;

import java.awt.peer.LightweightPeer;
import java.util.Date;
import java.util.List;

import org.hibernate.Query;

import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.cpo.domain.Login;
import com.cpo.domain.User;
import com.cpo.interfaces.ILoginDao;

@Repository
@Transactional
public class LoginDao implements ILoginDao{

	@Autowired
	private SessionFactory sessionFactory;
	
	public List<Login> getByUserName(String userName) {
	Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Login.class);
	criteria.add(Restrictions.ilike("userName", userName));
		return  criteria.list();
	}

	public void updateIncorrectAttempts(Login login) {
		
		login.setLastFailedDateTime(new Date());
		sessionFactory.getCurrentSession().merge(login);
	}

	public void lockAccount(String userName) {
		
		List<Login> login = getByUserName(userName);
		for(Login login1:login){
			login1.setStatus("L");
			sessionFactory.getCurrentSession().merge(login1);
		}
		
		
		
	}
/*	public List<Login> getUser(Login login){
		
		Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Login.class);
		return  criteria.list();
	}*/

	public void unlockAccount(Login login){
		sessionFactory.getCurrentSession().merge(login);
	}
	

}
